package io.gbznt.modules.buss.controller;

import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.serotonin.modbus4j.BatchResults;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;

import freemarker.template.utility.StringUtil;
import io.gbznt.modules.buss.entity.BussSwitchEntity;
import io.gbznt.modules.buss.entity.BussWaterpumEntity;
import io.gbznt.modules.buss.modbus.Modbus4jUtils;
import io.gbznt.modules.buss.modbus.Modbus4jUtils2;
import io.gbznt.modules.buss.service.BussSwitchService;
import io.gbznt.modules.buss.service.BussWaterpumService;
import io.gbznt.modules.buss.service.BussWeatherRecordService;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;



/**
 * 水泵表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-05-22 11:33:52
 */
@RestController
@RequestMapping("buss/busswaterpum")
public class BussWaterpumController {
    @Autowired
    private BussWaterpumService bussWaterpumService;
    
    @Autowired
    private BussSwitchService bussswitchService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("buss:busswaterpum:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bussWaterpumService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("buss:busswaterpum:info")
    public R info(@PathVariable("id") String id){
		BussWaterpumEntity bussWaterpum = bussWaterpumService.getById(id);

        return R.ok().put("bussWaterpum", bussWaterpum);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("buss:busswaterpum:save")
    public R save(@RequestBody BussWaterpumEntity bussWaterpum){
		bussWaterpumService.save(bussWaterpum);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("buss:busswaterpum:update")
    public R update(@RequestBody BussWaterpumEntity bussWaterpum){
		bussWaterpumService.updateById(bussWaterpum);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("buss:busswaterpum:delete")
    public R delete(@RequestBody String[] ids){
		bussWaterpumService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    
    /**
     * 删除
     */
    @RequestMapping("/openpum")
    //@RequiresPermissions("buss:busswaterpum:delete")
    public R openpum(@RequestBody Map<String, Object> nm){
    	String sw = (String) nm.get("sw");
    	String pum = (String) nm.get("pum");
    	int status =  (int) nm.get("status");
    	
    	try {
			//Modbus4jUtils2.writeHoldingRegister();
    		Modbus4jUtils2.writeHoldingRegister(1,Integer.parseInt(sw)-1, status, DataType.TWO_BYTE_INT_SIGNED);
    		Modbus4jUtils2.writeHoldingRegister(1,Integer.parseInt(pum)-1, status, DataType.TWO_BYTE_INT_SIGNED);
    		
    		BatchResults<Integer> results = Modbus4jUtils.batchReaddx();
			System.out.print(results);
			
			short[] arrayRefVar = new short[20];
			for (int i = 0; i < arrayRefVar.length; i++) {
				if(results.getValue(i) != null && results.getValue(i) != ""){
					arrayRefVar[i] =  (short) results.getValue(i);
				}else{
					arrayRefVar[i] =  0;
				}
				
		      }
			byte s = (byte) status;
			arrayRefVar[Integer.parseInt(sw)-1] = s;
			arrayRefVar[Integer.parseInt(pum)-1] = s;
			
			
			short switch1 = arrayRefVar[0];
			short switch2 = arrayRefVar[1];
			short switch3 = arrayRefVar[2];
			short switch4 = arrayRefVar[3];
			short switch5 = arrayRefVar[4];
			short switch6 = arrayRefVar[5];
			short switch7 = arrayRefVar[6];
			short switch8 = arrayRefVar[7];
			short switch9 = arrayRefVar[8];
			short switch10 = arrayRefVar[9];
			short switch11 = arrayRefVar[10];
			short switch12 = arrayRefVar[11];
			short switch13 = arrayRefVar[12];
			short switch14 = arrayRefVar[13];
			short switch15 = arrayRefVar[14];
			short switch16 = arrayRefVar[15];
			short switch17 = arrayRefVar[16];
			short switch18 = arrayRefVar[17];
			short switch19 = arrayRefVar[18];
			short switch20 = arrayRefVar[19];
			
			BussSwitchEntity be = new BussSwitchEntity();
			
			be.setSwitch1((int)switch1);
			be.setSwitch2((int)switch2);
			be.setSwitch3((int)switch3);
			be.setSwitch4((int)switch4);
			be.setSwitch5((int)switch5);
			be.setSwitch6((int)switch6);
			be.setSwitch7((int)switch7);
			be.setSwitch8((int)switch8);
			be.setSwitch9((int)switch9);
			be.setSwitch10((int)switch10);
			be.setSwitch11((int)switch11);
			be.setSwitch12((int)switch12);
			be.setSwitch13((int)switch13);
			be.setSwitch14((int)switch14);
			be.setSwitch15((int)switch15);
			be.setSwitch16((int)switch16);
			be.setSwitch17((int)switch17);
			be.setSwitch18((int)switch18);
			be.setSwitch19((int)switch19);
			be.setSwitch20((int)switch20);
			be.setCreateTime(new Date());
			bussswitchService.save(be);
    		
    		
		} catch (ModbusTransportException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			 return R.error();
		} catch (ErrorResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return R.error();
		} catch (ModbusInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return R.error();
		}

        return R.ok();
    }
     
    public String name() {
		
    	return "";
	}
}
